package com.test.mylotterysystem.service.Impl;

import com.test.mylotterysystem.comment.errorCode.ServiceErrorCode;
import com.test.mylotterysystem.comment.exception.ServiceException;
import com.test.mylotterysystem.comment.utils.JacksonUtil;
import com.test.mylotterysystem.constants.Constants;
import com.test.mylotterysystem.controller.Param.DrawPrizeParam;
import com.test.mylotterysystem.service.DrawPrizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class DrawPrizeServiceImpl implements DrawPrizeService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 将前端传递过来的数据存储到rabbitMQ中
     *
     * @param drawPrizeParam
     * @return
     */
    @Override
    public void drawPrize(DrawPrizeParam drawPrizeParam) {
        if (drawPrizeParam == null) {
            throw new ServiceException(ServiceErrorCode.DRAW_PRIZE_PARAM_IS_EMPTY);
        }

        Map<String, String> map = new HashMap<>();
        map.put("messageId", UUID.randomUUID().toString().replace("-", ""));
        map.put("message", JacksonUtil.writeValueAsString(drawPrizeParam));
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_NAME, Constants.KEY, map);

        log.info("消息传递成功");
    }
}
